home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / utility.lha / utility / pool.H < prev    next >
C/C++ Source or Header  |  1993-08-08  |  2KB  |  86 lines

  1. // This is a fast memory allocator modelled after class Pool in the
  2. // AT&T Standard Library Extension.
  3. //
  4. // Every Pool is a collection of elements, each of which is a chunk of
  5. // contigous memory of unspecified type.  All elements of a Pool are
  6. // the same size.
  7. //
  8. // If "n" is an unsigned integer, the constructor Pool(n) generates
  9. // a pool whose elements are of size "n".  Destoying a Pool frees all
  10. // the memory occupied by its elements.
  11. //
  12. // .SS Performance
  13. // The use of pools substantially reduces the number of calls to malloc().
  14. // Allocation of a new element is done inline, except when a new
  15. // block of elements must be allocated.
  16. //
  17. // .SS Author
  18. // Dag Bruck, Department of Automatic Control, Lund Institute of Technology,
  19. // Box 118, S-221 00 Lund, Sweden (dag@control.lth.se).
  20. //
  21. // $Id: pool.H,v 1.2 91/09/06 16:59:03 dag Exp $
  22.  
  23.  
  24. #ifndef POOL_H
  25. #define POOL_H
  26.  
  27.  
  28. class Pool {
  29. public:
  30.   Pool(unsigned n);
  31.   // Creates a pool whose elements are of size "n".
  32.  
  33.   void* alloc();
  34.   // A new element is allocated in the pool.  The result is a pointer
  35.   // to the element.
  36.  
  37.   void free(void* vp);
  38.   // The element of the pool addressed by "vp" is freed.  The element must
  39.   // have been allocated from this pool; if not the results are undefined.
  40.  
  41.   ~Pool();
  42.   // Returns all memory allocated by the pool to the system.
  43.  
  44. private:
  45.   struct Pool_Element { Pool_Element* next; };
  46.   // The beginning of a pool element, as seen in the free list.
  47.   
  48.   struct Pool_Block { Pool_Block* next; Pool_Element pe; };
  49.   // The beginning of a block of allocated pool memory.
  50.   
  51.   Pool_Element* free_list;
  52.   // The list of free pool elements.
  53.  
  54.   unsigned sz;
  55.   // The size of the pool elements, in bytes.  This number is rounded up
  56.   // to a multiple of some small number (to ensure alignment).
  57.  
  58.   Pool_Block* blockp;
  59.   // List of memory blocks allocated by alloc_block().
  60.  
  61.   void alloc_block();
  62.   // Allocates a new block of memory and splits it up in elements
  63.   // that are inserted into the free list.
  64. };
  65.  
  66.  
  67. inline void* Pool :: alloc()
  68. {
  69.   if (free_list == 0)
  70.     alloc_block();
  71.   register Pool_Element* pe = free_list;
  72.   free_list = pe->next;
  73.   return pe;
  74. }
  75.  
  76.  
  77. inline void Pool :: free(void* p)
  78. {
  79.   register Pool_Element* pe = (Pool_Element *) p;
  80.   pe->next = free_list;
  81.   free_list = pe;
  82. }
  83.  
  84.  
  85. #endif
  86.